#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "win_com.h"

#include "control.h"
/*******************************************************************************
*	WINDOW's objects declaration
*******************************************************************************/

extern CONTAINER 	g_win_timermsg;
extern TEXT_FIELD    timer_msg_txt;

static VACTION  timermsg_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT 	timermsg_proc(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

#define WIN_SH_IDX        WSTL_POP_WIN_01_HD
#define TXT_SH_IDX        WSTL_TEXT_04_HD


#define	W_L     100
#define	W_T     200
#define	W_W     400
#define	W_H     80

#define TEXT_L  (W_L + 10)
#define TEXT_T  (W_T + 10)
#define TEXT_W  (W_W - 20)
#define TEXT_H  (W_H - 20)

#define LDEF_TXT_MSG(root,varTxt,nxtObj,l,t,w,h,resID,str)	\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_SH_IDX,TXT_SH_IDX,TXT_SH_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    timermsg_keymap,timermsg_proc,  \
    nxtObj, focusID,0)


LDEF_TXT_MSG(&g_win_timermsg, timer_msg_txt, NULL, TEXT_L,TEXT_T,TEXT_W,TEXT_H, 0, len_display_str)
LDEF_WIN(g_win_timermsg, &timer_msg_txt, W_L, W_T, W_W, W_H, 1)


/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/
VSCR 	g_vscr_back;
UINT8* 			timer_msg_osd_buf;
#define OSD_BUF_SIZE ( (W_W + 4)*W_H )
/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION  timermsg_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
    switch(key)
    {
        case V_KEY_EXIT:
        case V_KEY_MENU:
        case V_KEY_ENTER:
            act = VACT_CLOSE;
			break;
        default:
            act = VACT_PASS;
    }

	return act;
}


static PRESULT 	timermsg_proc(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	switch(event)
	{
	case EVN_PRE_OPEN:
	case EVN_POST_OPEN:
	case EVN_PRE_CLOSE:
	case EVN_POST_CLOSE:
		break;
	}

	return ret;	
}

void timermsg_show(char* msg)
{
	UINT32 hkey,vkey;
	TEXT_FIELD *txt;
	
    BOOL old_value = ap_enable_key_task_get_key(TRUE);

	wincom_backup_region(&g_win_timermsg.head.frame);
	txt = &timer_msg_txt;
	OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)msg);
	
	
	OSD_TrackObject((POBJECT_HEAD)&g_win_timermsg, C_UPDATE_ALL);

	while(1)
	{
	
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
			continue;

		ap_hk_to_vk(0, hkey, &vkey);

		if(vkey == V_KEY_EXIT || vkey == V_KEY_MENU || vkey == V_KEY_ENTER)
			break;
	}
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);
}

